Component({
  properties: {
    text: {
      type: String,
      value: ''
    },
    Img1: {
      type: String,
      value: ''
    },
    Img2: {
      type: String,
      value: ''
    }
  },
  observers: {
    "text": function(newValue) {
      if(newValue) {
        this.setData({ customizedText: newValue });
      }
    },
    "Img1": function(newSrc) {
      if(newSrc) {
        this.setData({ customizedSearchImage: newSrc});
      }
    },
    "Img2": function(newSrc) {
      if(newSrc) {
        this.setData({ customizedSpeechImage: newSrc, isShowSpeech: true })
      }
    }
  },
  data: {
    customizedText: '',
    customizedSpeechImage: '',
    isShowSpeech: false,
    isShowText: false,
    navBarHeight: 0,
    menuRight: 0,
    menuTop: 0,
    menuHeight: 0,
    menuWidth: 0,
    customizedSearchImage: ""
  },
  methods: {
    onSearchTab() {
      // this.triggerEvent('tap-search');
      console.log('tap-search')

      if(wx.getStorageSync('accessTokenData').accessToken){
        wx.navigateTo({
            url: '/pages/search/search',
        })
      }
      else{
        wx.showToast({
          title: '请前往“我的”授权登录！',
          icon: "none",
          duration: 2000
        })
      }
    },
    // util function
    getNavBarInfo() {
      const sysInfo = wx.getSystemInfoSync();
      const menuInfo = wx.getMenuButtonBoundingClientRect();

      const navBarInfo = {
        navBarHeight: (sysInfo.statusBarHeight + menuInfo.height+ (menuInfo.top - sysInfo.statusBarHeight) * 2)  * (750 / sysInfo.windowWidth),
        menuRight: (sysInfo.screenWidth - menuInfo.right) * (750 / sysInfo.windowWidth),
        menuTop: (menuInfo.top)  * (750 / sysInfo.windowWidth),
        menuHeight: menuInfo.height  * (750 / sysInfo.windowWidth),
        menuWidth: menuInfo.width  * (750 / sysInfo.windowWidth)
      }
      // console.log(sysInfo.statusBarHeight, menuInfo.height, menuInfo.top)
      this.setNavBarInfo(navBarInfo);
      // store in localstorage
      wx.setStorage({
        key: 'navBarInfo',
        data: navBarInfo
      });
    },
    setNavBarInfo(navBarInfo) {
      this.setData({ 
        navBarHeight: navBarInfo.navBarHeight,
        menuRight: navBarInfo.menuRight ,
        menuTop: navBarInfo.menuTop,
        menuHeight: navBarInfo.menuHeight ,
        menuWidth: navBarInfo.menuWidth
       });
    }
  },
  lifetimes: {
    attached() {
      // wx.getStorage({ key: 'navBarInfo' })
      // .then((res) => {
      //   if(res.data) { this.setNavBarInfo(res.data) }
      // })
      // .catch(() => {
      //   this.getNavBarInfo();
      // })
      this.getNavBarInfo();
    }
  }
})
